using UnityEngine;
using QFramework;
using UnityEngine.UI;

namespace QFrameworkSurvivor
{
	public partial class FloatingTextController : ViewController
	{
		private static FloatingTextController mDefault;
        private void Awake()
        {
            mDefault = this;
        }
        public static void Play(Vector2 position, string text, bool critical = false)
        {
            mDefault.FloatingText.InstantiateWithParent(mDefault.transform)
                .Position(position)
                .Self(f => 
                {
                    var PositionY=position.y;
                    var TextTrans = f.transform.Find("Text");
                    var TextComp= TextTrans.GetComponent<Text>();
                    TextComp.text = text;

                    if(critical )
                    {
                        TextComp.color = Color.red;
                    }

                    ActionKit.Sequence()
                    .Lerp(0, 0.5f, 0.5f, (p) =>
                    {
                        f.PositionY(PositionY + p * 0.25f);
                        TextComp.LocalScaleX(Mathf.Clamp01(p * 8));
                        TextComp.LocalScaleY(Mathf.Clamp01(p * 8));
                    })
                    .Delay(0.5f)
                    .Lerp(1.0f,0, 0.3f, (p) =>
                    {
                        TextComp.ColorAlpha(p);
                    },() =>{ TextTrans.DestroyGameObjGracefully();})
                    .Start(TextComp);
                }).Show();
        }
        void Start()
		{
			// Code Here
            FloatingText.Hide();
		}
        private void OnDestroy()
        {
            mDefault = null;
        }
    }
}
